{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "ba5aead6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "import time\n",
    "\n",
    "def openreadtxt(file_name):  #读取txt里的化学品名称\n",
    "    data = []\n",
    "    file = open(file_name,'r',encoding='utf-8')  #打开文件\n",
    "    file_data = file.readlines() #读取所有行\n",
    "    for row in file_data:\n",
    "        row=row.replace('\\n', '')\n",
    "       # tmp_list = row.split(' ') #按‘，'切分每行的数据\n",
    "       # tmp_list[-1] = tmp_list[-1].replace('\\n',',') #去掉换行符\n",
    "        data.append(row) #将每行数据插入data中\n",
    "    return data\n",
    "\n",
    "def getMsdsUrl(compoundName):\n",
    "    url=\"\"\n",
    "    driver.get(\"http://www.somsds.com/\")\n",
    "    text_box = driver.find_element(by=By.NAME, value=\"form\")\n",
    "    input_box = text_box.find_element(by=By.NAME, value=\"keyword\")\n",
    "    submit_button = text_box.find_element(by=By.NAME, value=\"Submit\")\n",
    "    input_box.send_keys(compoundName)\n",
    "    submit_button.click()\n",
    "    driver.switch_to.window(driver.window_handles[1]) # 切换到最后一个窗口\n",
    "    try:\n",
    "        div_box = driver.find_element(by=By.CLASS_NAME, value=\"ms_l\")\n",
    "        table = div_box.find_element(By.XPATH, \"//table/tbody/tr[2]/td/div/a\")\n",
    "        url=table.get_attribute('href')\n",
    "    except:\n",
    "        print(compoundName+\"  错误\")\n",
    "    driver.close()\n",
    "    driver.switch_to.window(driver.window_handles[-1]) # 切换到最后一个窗口\n",
    "    driver.close()\n",
    "    return url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "19fa5f9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.somsds.com/detail.asp?id=-83153750\n",
      "http://www.somsds.com/detail.asp?id=1032491871\n"
     ]
    }
   ],
   "source": [
    "compoundNames=openreadtxt(\"test.txt\")\n",
    "for compoundName in compoundNames:\n",
    "    \n",
    "    driver = webdriver.Chrome()\n",
    "    url=getMsdsUrl(compoundName)\n",
    "    \n",
    "    driver = webdriver.Chrome()\n",
    "    driver.get(url)\n",
    "    time.sleep(2)\n",
    "    \n",
    "    name_html=compoundName+\".html\"\n",
    "    f = open(name_html,'wb')\n",
    "    f.write(driver.page_source.encode(\"gbk\", \"ignore\")) # 忽略非法字符\n",
    "    print(url)\n",
    "    f.close() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b64723d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
